package com.hand.wfive.adapter

import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.transition.Fade
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hand.wfive.R
import com.hand.wfive.entity.FileModel
import com.hand.wfive.entity.Params
import com.hand.wfive.entity.PickerFileModel
import com.hand.wfive.loginAndVip.model.ActivityCouponModel
import com.hand.wfive.util.FileUtils
import com.hand.wfive.util.oss.OssRequest
import com.qmuiteam.qmui.util.QMUIDisplayHelper

/**
 * 选择文件
 */
class PickerFileAdapter : BaseQuickAdapter<PickerFileModel, BaseViewHolder>(
    R.layout.item_file_select
), LoadMoreModule {

    override fun convert(holder: BaseViewHolder, item: PickerFileModel) {
        when (item.type) {
            Params.pickerWord -> holder.setImageResource(R.id.iv_logo, R.mipmap.ic_file_item_word)
            Params.pickerPdf -> holder.setImageResource(R.id.iv_logo, R.mipmap.ic_file_item_pdf)
            else -> holder.setImageResource(R.id.iv_logo, R.mipmap.ic_file_item_image)
        }
        holder.setText(R.id.tv_name, item.name)
        holder.setText(R.id.tv_time, item.time)
        holder.setVisible(R.id.v_line, getItemPosition(item) != itemCount - 1)
        if (item.select) {
            holder.setImageResource(R.id.iv_more, R.mipmap.ic_file_edit_select)
        } else {
            holder.setImageResource(R.id.iv_more, R.mipmap.ic_file_edit_un_select)
        }
    }
}

class FileEditAdapter :
    BaseQuickAdapter<FileModel, BaseViewHolder>(R.layout.item_file), LoadMoreModule {
    override fun convert(holder: BaseViewHolder, item: FileModel) {
        val view = holder.getView<ImageView>(R.id.iv_logo)
        when (item.type) {
            Params.fileTypeWord -> Glide.with(view)
                .load(OssRequest.getInstance().getFileUrl(item.coverFileId))
                .placeholder(R.mipmap.ic_file_item_pdf).into(view)

            Params.fileTypePdf -> Glide.with(view)
                .load(OssRequest.getInstance().getFileUrl(item.coverFileId))
                .placeholder(R.mipmap.ic_file_item_pdf).into(view)

            else -> {
                var fileIds = item.fileId
                if (fileIds.contains(",")) {
                    val splits = fileIds.split(",")
                    fileIds = splits[0]
                }
                Glide.with(view).load(OssRequest.getInstance().getFileUrl(fileIds))
                    .placeholder(R.mipmap.ic_file_item_pdf).into(view)
            }

        }


        holder.setText(R.id.tv_name, item.fileName)
        holder.setText(R.id.tv_time, "${item.updateTime}  ${FileUtils.convertFileSize(item.size)}")
        //        holder.setText(R.id.tv_size, "文件大小：${FileUtils.convertFileSize(item.size)}")
        holder.setText(R.id.tv_status, if ("2" == item.status) "已签" else "待签")
        holder.setVisible(R.id.v_line, getItemPosition(item) != itemCount - 1)
    }
}

class FileSelectAdapter :
    BaseQuickAdapter<FileModel, BaseViewHolder>(R.layout.item_file_select), LoadMoreModule {
    override fun convert(holder: BaseViewHolder, item: FileModel) {
        val view = holder.getView<ImageView>(R.id.iv_logo)
        when (item.type) {
            Params.fileTypeWord -> Glide.with(view)
                .load(OssRequest.getInstance().getFileUrl(item.coverFileId))
                .placeholder(R.mipmap.ic_file_item_pdf).into(view)

            Params.fileTypePdf -> Glide.with(view)
                .load(OssRequest.getInstance().getFileUrl(item.coverFileId))
                .placeholder(R.mipmap.ic_file_item_pdf).into(view)

            else -> {
                var fileIds = item.fileId
                if (fileIds.contains(",")) {
                    val splits = fileIds.split(",")
                    fileIds = splits[0]
                }
                Glide.with(view).load(OssRequest.getInstance().getFileUrl(fileIds))
                    .placeholder(R.mipmap.ic_file_item_pdf).into(view)
            }

        }
        if (item.select) {
            holder.setImageResource(R.id.iv_more, R.mipmap.ic_file_edit_select)
        } else {
            holder.setImageResource(R.id.iv_more, R.mipmap.ic_file_edit_un_select)
        }


        holder.setText(R.id.tv_name, item.fileName)
        holder.setText(R.id.tv_time, "${item.updateTime}")
        holder.setVisible(R.id.v_line, getItemPosition(item) != itemCount - 1)
    }
}

/**
 * 文件
 */
class FileAdapter :
    BaseQuickAdapter<FileModel, BaseViewHolder>(R.layout.item_file), LoadMoreModule {
    override fun convert(holder: BaseViewHolder, item: FileModel) {
        val view = holder.getView<ImageView>(R.id.iv_logo)
        when (item.type) {
            Params.fileTypeWord -> Glide.with(view)
                .load(OssRequest.getInstance().getFileUrl(item.coverFileId))
                .placeholder(R.mipmap.ic_file_item_pdf).into(view)

            Params.fileTypePdf -> Glide.with(view)
                .load(OssRequest.getInstance().getFileUrl(item.coverFileId))
                .placeholder(R.mipmap.ic_file_item_pdf).into(view)

            else -> {
                var fileIds = item.fileId
                if (fileIds.contains(",")) {
                    val splits = fileIds.split(",")
                    fileIds = splits[0]
                }
                Glide.with(view).load(OssRequest.getInstance().getFileUrl(fileIds))
                    .placeholder(R.mipmap.ic_file_item_pdf).into(view)
            }

        }


        holder.setText(R.id.tv_name, item.fileName)
        holder.setText(R.id.tv_time, "${item.updateTime}")
//        holder.setText(R.id.tv_time, "${item.updateTime}  ${FileUtils.convertFileSize(item.size)}")
//        holder.setText(R.id.tv_size, "文件大小：${FileUtils.convertFileSize(item.size)}")
        holder.setText(R.id.tv_status, if ("2" == item.status) "已签" else "待签")
        holder.setVisible(R.id.v_line, getItemPosition(item) != itemCount - 1)
    }
}


/**
 * 签字库
 */
class SignLibraryAdapter(
    private val mType: String
) : BaseQuickAdapter<FileModel, BaseViewHolder>(R.layout.item_sign_library2), LoadMoreModule {
    override fun convert(holder: BaseViewHolder, item: FileModel) {
        val rl_item = holder.getView<View>(R.id.rl_item)
//        val bgImage = holder.getView<ImageView>(R.id.iv_bg)
        val signImage = holder.getView<ImageView>(R.id.iv_sign)
        if (mType == Params.fileTypeSign) {
            holder.setText(R.id.tv_title, "添加签名")
        } else {
            holder.setText(R.id.tv_title, "添加签章")
        }
        val position = getItemPosition(item)
        if (position == 0) {
            holder.setGone(R.id.ll_add, false)
            holder.setGone(R.id.rl_image, true)
//            signImage.isVisible = false
//            holder.setGone(R.id.cl_bottom, true)
            rl_item.setBackgroundResource(R.drawable.bg_sign_item1)
//            if (mType == Params.fileTypeSign) {
//
//                bgImage.setBackgroundResource(R.mipmap.ic_sign_library_add_signature)
//            } else {
//                bgImage.setBackgroundResource(R.mipmap.ic_sign_library_add_stamp)
//            }
        } else {
            rl_item.setBackgroundResource(R.drawable.bg_sign_item)
            holder.setGone(R.id.ll_add, true)
            holder.setGone(R.id.rl_image, false)
//            signImage.isVisible = true
            Glide.with(context).load(OssRequest.getInstance().getFileUrl(item.fileId))
                .override(QMUIDisplayHelper.dp2px(context, 100))
                .into(signImage)
//
//            holder.setText(R.id.tv_name, item.fileName)
//            holder.setGone(R.id.cl_bottom, false)
//
//            bgImage.setBackgroundResource(R.drawable.shape_white_r8)
        }
    }
}

class DialogSignAdapter(
    private val mType: String
) : BaseQuickAdapter<FileModel, BaseViewHolder>(R.layout.item_dialog_sign), LoadMoreModule {
    override fun convert(holder: BaseViewHolder, item: FileModel) {
        val bgImage = holder.getView<ImageView>(R.id.iv_bg)
        val nameText = holder.getView<TextView>(R.id.tv_name)
        val signImage = holder.getView<ImageView>(R.id.iv_sign)
        val position = getItemPosition(item)
        if (position == 0) {
            nameText.isVisible = false
            signImage.isVisible = false
            if (mType == Params.fileTypeSign) {
                bgImage.setBackgroundResource(R.mipmap.ic_sign_library_add_signature)
            } else {
                bgImage.setBackgroundResource(R.mipmap.ic_sign_library_add_stamp)
            }
        } else {
            nameText.isVisible = true
            signImage.isVisible = true
            Glide.with(context).load(OssRequest.getInstance().getFileUrl(item.fileId))
                .into(holder.getView(R.id.iv_sign))
            holder.setText(R.id.tv_name, item.fileName)
        }
    }
}


/**
 * 手写签名颜色
 */
class SignatureWriteColorAdapter : BaseQuickAdapter<Int, BaseViewHolder>(
    R.layout.item_signature_write_color, Params.signColor
) {
    override fun convert(holder: BaseViewHolder, item: Int) {
        holder.setBackgroundColor(R.id.qiv2_color, item)
    }
}

/**
 * 签章颜色
 */
class StampColorAdapter : BaseCheckPositionAdapter<Int, BaseViewHolder>(
    R.layout.item_stamp_color, Params.signColor
) {
    init {
        baseCheckPosition = 0
    }

    override fun convert(holder: BaseViewHolder, item: Int) {
        holder.getView<ImageView>(R.id.iv_color).setColorFilter(item)
        holder.setVisible(R.id.iv_check, getItemPosition(item) == baseCheckPosition)
    }
}

/**
 * 会员返回（优惠券）
 */
//class VipBackCounpAdapter(data: MutableList<ActivityCouponModel>) :
//    BaseQuickAdapter<ActivityCouponModel, BaseViewHolder>(
//        R.layout.login_item_vip_back_coupon, data
//    ) {
//    override fun convert(holder: BaseViewHolder, item: ActivityCouponModel) {
//        Glide.with(context).load(item.retUrl).into(holder.getView(R.id.iv_coupon))
//        holder.setGone(R.id.iv_line, getItemPosition(item) == itemCount - 1)
//    }
//}